import styles from './index.less';
import React, { PureComponent } from 'react';
import { PropTypes } from 'prop-types';
import cookies from 'react-cookies';
import Link from 'umi/link';
import { getDocumentCookie } from '../../util/utils'

class Menus extends PureComponent {
  static defaultProps = {
    islogin:false,
    username:'',
    userurl:'',
  };
  static propTypes = {
    islogin: PropTypes.bool,
    username:PropTypes.string,
    userurl:PropTypes.string,
  };
  constructor(props) {
      super(props);    
      this.state = {
        username:this.props.username,
        userurl:this.props.userurl,
        islogin: this.props.islogin
    };
  }
  
  componentDidMount() {
    this.getCookie();
  }
  getCookie(){
    //根据cookie判断用户是否登录
    var cookiepre = getDocumentCookie();
    if (cookiepre != '') {
      var login_userinfo = cookies.load(cookiepre);//获取当前登录的用户信息
      if (login_userinfo && typeof(login_userinfo) == 'object' &&
        login_userinfo['uid'] && login_userinfo['username']) {
        //获取uid值，并存入到cookie里
        cookies.save('uid',login_userinfo['uid']);
        this.setState({
          username : login_userinfo['username'],
          userurl : '//bbs.360.cn/home.php?mod=space&uid='+login_userinfo['uid'],
          islogin : true
        })
      } else {
        this.setState({
          username : '',
          userurl : '',
          islogin : false
        })
      }
    }
  }
  render() {
    
    return (
      <div className={styles.top_header}>
        <div className={styles.logos}><Link  to="/"></Link></div>
        <div className={styles.personinfo}>
          {
            this.state.islogin 
          ? <div>
              <a className={styles.username} target="_blank" url={this.state.userurl} >{this.state.username}</a> 
              <a  className={styles.userlogout} href="//passport.qiku.com/sso/logout?appid=1010000675&callback=//bbs.360.cn/zt/photowall/#/">退出</a> 
            </div>
          : <div>
              <a className={styles.personlog} href="//bbs.360.cn/member.php?mod=logging&amp;action=login">登陆</a>  
              <a  className={styles.personreg} href="//passport.qiku.com/register?appid=1010000675&callback=http%3a%2f%2fbbs.360.cn%2flogin_sso_web.php">注册</a> 
            </div>
          }
        </div>
        <div className={styles.nav_header}>
          <ul className={styles.nav}>
            <li>
              <Link  to="/">首页</Link>
            </li>
            <li>
              <a href="//i360mall.com" target="_blank">360商城</a>
            </li>
            <li>
              <a href="//bbs.360.cn/" target="_blank">360社区</a>
            </li>
          </ul>
        </div>
      </div>
    );
  }
  
};

export default Menus;

